package com.tuanzi.loan.web.bean.family;

import com.tuanzi.loan.business.service.family.IcpWebCommentService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFListingBean;

import com.tuanzi.loan.vo.request.family.IcpWebCommentSearchRequest;
import com.tuanzi.loan.vo.response.family.IcpWebCommentSearchResponse;
import com.tuanzi.loan.web.lazy.family.IcpWebCommentDataModel;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

/**
 * ICP备案网站评论管理
 * @author JinRuiyang 2017/12/13 11:41:18
 */
@Debug
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class IcpWebCommentBean extends JSFListingBean<IcpWebCommentSearchRequest, IcpWebCommentSearchResponse> {

    @Autowired
    private IcpWebCommentService icpWebCommentService;

    @PostConstruct
    private void init() {
        initLazyDataModel();
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onRefresh(){
        lazyDataModel = new IcpWebCommentDataModel($);
        addSuccessfullyMessage();
    }

    public void onPublish(Long id){
        try {
            icpWebCommentService.updatePublishStatus(id, IcpWebCommentService.Published);
        } catch (RuntimeException e){
            addErrorMessage(e.getMessage());
        }
    }

    public void onBatchPublish(){
        for (IcpWebCommentSearchResponse response : selectedRecords){
            onPublish(response.getPk());
        }
    }

    public void onDelete(Long id){
        try {
            icpWebCommentService.updateEnableStatus(id, IcpWebCommentService.Disabled);
        } catch (RuntimeException e){
            addErrorMessage(e.getMessage());
        }
    }

    public void onBatchDelete(){
        for (IcpWebCommentSearchResponse response : selectedRecords){
            onDelete(response.getPk());
        }
    }

    private void initLazyDataModel() {
        $ = new IcpWebCommentSearchRequest();
        lazyDataModel = new IcpWebCommentDataModel($);
    }
}